package com.hp.gxw.api;

import com.alibaba.fastjson.JSON;
import com.hp.gxw.constant.R;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.Rdrecord11Parameter;
import com.hp.gxw.u8.dto.Rdrecords11Parameter;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.xml.Rdrecord11Xml;
import com.hp.gxw.utils.DateUtil;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RequestMapping("/api/rdrecord11")
public class RdRecord11Api {

    @Autowired
    private U8ApiUtils u8ApiUtils;

	@Autowired
	private EFWebService eFWebServiceLocator;

    @Value("${u8client.serviceId}")
    private String serviceId;

    @Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R rdrecord11_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        Rdrecord11Parameter rdrecord11Parameter = new Rdrecord11Parameter();
        if(map.get("ccode") != null){
            rdrecord11Parameter.setCcode(map.get("ccode").toString());
        }else{
            rdrecord11Parameter.setCcode("0000000001");
        }
        if(map.get("brdflag") == null){
            return R.error("brdflag为必填项");
        }
        rdrecord11Parameter.setBrdflag(map.get("brdflag").toString());//采购类型
        if(map.get("cvouchtype") != null){
            rdrecord11Parameter.setCvouchtype(map.get("cvouchtype").toString());
        }else{
            rdrecord11Parameter.setCvouchtype("11");
        }
        if(map.get("crdcode") == null){
            return R.error("crdcode为必填项");
        }
        rdrecord11Parameter.setCrdcode(map.get("crdcode").toString());//采购类型
        if(map.get("cwhcode") == null){
            return R.error("cwhcode为必填项");
        }
        rdrecord11Parameter.setCwhcode(map.get("cwhcode").toString());//采购类型
        if(map.get("csource") != null){
            rdrecord11Parameter.setCsource(map.get("csource").toString());
        }else{
            rdrecord11Parameter.setCsource("库存");
        }
        if(map.get("cbustype") != null){
            rdrecord11Parameter.setCbustype(map.get("cbustype").toString());
        }else{
            rdrecord11Parameter.setCbustype("领料");
        }
        if(map.get("cdepcode") != null){
            rdrecord11Parameter.setCdepcode(map.get("cdepcode").toString());
        }
        if(map.get("cmaker") != null){
            rdrecord11Parameter.setCmaker(map.get("cmaker").toString());
        }else{
            rdrecord11Parameter.setCmaker("api");
        }
        if(map.get("ddate") != null){
            rdrecord11Parameter.setDdate(DateUtil.getDateFormat(map.get("ddate").toString()));
        }else{
            rdrecord11Parameter.setDdate(new Date());
        }
        if(map.get("dnmaketime") != null){
            rdrecord11Parameter.setDnmaketime(DateUtil.getDateTimeFormat(map.get("dnmaketime").toString()));
        }else{
            rdrecord11Parameter.setDnmaketime(new Date());
        }
        if(map.get("chandler") != null){
            rdrecord11Parameter.setChandler(map.get("chandler").toString());
        }else{
            rdrecord11Parameter.setChandler("api");
        }
        if(map.get("dveridate") != null){
            rdrecord11Parameter.setDveridate(DateUtil.getDateFormat(map.get("dveridate").toString()));
        }else{
            rdrecord11Parameter.setDveridate(new Date());
        }
        if(map.get("dnverifytime") != null){
            rdrecord11Parameter.setDnverifytime(DateUtil.getDateTimeFormat(map.get("dnverifytime").toString()));
        }else{
            rdrecord11Parameter.setDnverifytime(new Date());
        }

        if(map.get("details") == null){
            return R.error("表体详情数据不能为空");
        }
        List<Rdrecords11Parameter> rdrecords01ParameterList = JSON.parseArray(JSON.toJSON(map.get("details")).toString(), Rdrecords11Parameter.class);
        for(int i=0; i<rdrecords01ParameterList.size(); i++){
            if(rdrecords01ParameterList.get(i).getCinvcode() == null){
                return R.error("表体详情数据第"+i+1+"行cinvcode为空");
            }
            if(rdrecords01ParameterList.get(i).getIquantity() == null || rdrecords01ParameterList.get(i).getIquantity().compareTo(BigDecimal.ZERO) == 0){
                return R.error("表体详情数据第"+i+1+"行iquantity为空或者为0");
            }
        }
        rdrecord11Parameter.setDetailsList(rdrecords01ParameterList);
        rdrecord11Parameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(Rdrecord11Xml.createXML(rdrecord11Parameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(Rdrecord11Xml.createXML(rdrecord11Parameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
